package com.pojo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hjb
 * @Version 1.0
 * @Contact 1754729389@qq.com
 */

/**
 * 购物车对象
 */
public class Cart {

    private Integer totalCount;
    private BigDecimal totalDeposit;
    private BigDecimal totalBasicPrice;
    private Map<Integer,CartItem> items = new LinkedHashMap<Integer,CartItem>();

    /**
     * 添加商品项
     * @param cartItem 商品项
     */
    public void addItem(CartItem cartItem) {

        //查看购物车是否已经已经添加过此商品
        CartItem item = items.get(cartItem.getId());
        if(item == null){
            items.put(cartItem.getId(),cartItem);
        }


    }

    /**
     * 根据id删除商品项
     * @param id 商品id
     */
    public void deleteItem(Integer id){

        items.remove(id);

    }

    public void clear(){
        items.clear();

    }

    @Override
    public String toString() {
        return "Cart{" +
                "totalCount=" + totalCount +
                ", totalDeposit=" + totalDeposit +
                ", totalBasicPrice=" + totalBasicPrice +
                ", items=" + items +
                '}';
    }

    public Integer getTotalCount() {
        totalCount = 0;

        for (Map.Entry<Integer,CartItem>entry : items.entrySet()) {
            totalCount += 1;
        }
        return totalCount;
    }


    public BigDecimal getTotalDeposit() {
        totalDeposit = new BigDecimal(0);
        for (Map.Entry<Integer,CartItem>entry : items.entrySet()) {
            totalDeposit = totalDeposit.add(entry.getValue().getDeposit());
        }
        return totalDeposit;
    }


    public BigDecimal getTotalBasicPrice() {
        totalBasicPrice = new BigDecimal(0);
        for (Map.Entry<Integer,CartItem>entry : items.entrySet()) {
            totalBasicPrice = totalBasicPrice.add(entry.getValue().getBasicPrice());
        }
        return totalBasicPrice;
    }


    public Map<Integer, CartItem> getItems() {
        return items;
    }

    public void setItems(Map<Integer, CartItem> items) {
        this.items = items;
    }
}
